package DAOs;

import DAOs.exceptions.NonexistentEntityException;
import Entity.SatellitalImage;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author USUARIO
 */
public class SatellitalImageJpaController implements Serializable {

    public SatellitalImageJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(SatellitalImage satellitalImage) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(satellitalImage);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(SatellitalImage satellitalImage) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            satellitalImage = em.merge(satellitalImage);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = satellitalImage.getId();
                if (findSatellitalImage(id) == null) {
                    throw new NonexistentEntityException("The satellitalImage with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            SatellitalImage satellitalImage;
            try {
                satellitalImage = em.getReference(SatellitalImage.class, id);
                satellitalImage.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The satellitalImage with id " + id + " no longer exists.", enfe);
            }
            em.remove(satellitalImage);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<SatellitalImage> findSatellitalImageEntities() {
        return findSatellitalImageEntities(true, -1, -1);
    }

    public List<SatellitalImage> findSatellitalImageEntities(int maxResults, int firstResult) {
        return findSatellitalImageEntities(false, maxResults, firstResult);
    }

    private List<SatellitalImage> findSatellitalImageEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(SatellitalImage.class));
            Query q = em.createQuery(cq);
            Query q1 = em.createNativeQuery("Select ID from satellitalimage");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
                q1.setMaxResults(maxResults);
                q1.setFirstResult(firstResult);
            }
            List<SatellitalImage> satellitalImagesList = q.getResultList();
            for (int i = 0; i < satellitalImagesList.size(); i++) {
                satellitalImagesList.get(i).setId((Long) q1.getResultList().get(i));
            }
            return satellitalImagesList;
        } finally {
            em.close();
        }
    }

    public SatellitalImage findSatellitalImage(Long id) {
        EntityManager em = getEntityManager();
        try {
            SatellitalImage satellitalImage = em.find(SatellitalImage.class, id);
            satellitalImage.setId(id);
            return satellitalImage;
        } finally {
            em.close();
        }
    }

    public int getSatellitalImageCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<SatellitalImage> rt = cq.from(SatellitalImage.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}
